import customTab from "../../../wxjs/customTab";
const app = getApp();

var data = {
  isLogin: false,
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  sportDataAppid:app.sportData.appid,
  sportDataPath: app.sportData.path,
  whochangeSportAppid: app.whochangeSport.appid,
  whochangeSportPath: app.whochangeSport.path,
  balanceAmout:0,
};

var currentPage = {
  data,
  onLoad: function(options) {
    const that = this;
    console.log(app.userInfo)
    // var myInfo = app.isNavigateToLogin();
    // if (!myInfo) return;

    // myInfo = app.isNavigateToBind();
    // if (!myInfo) return;
    app.xlogin.getUser(function (res) {
      var myInfo = app.isNavigateToLogin();
      if (!myInfo) return;

      myInfo = app.isNavigateToBind();
      if (!myInfo) return;

      app.infoLog("onShow isToOrder===>", app.isToOrder);
      that.getUser();
      that.setData({
        count: app.notifyCount
      })
      if (!app.notifyCount) {
        wx.hideTabBarRedDot({
          index: 2,
        })
      }
      if (app.isToOrder) {
        setTimeout(() => {
          wx.navigateTo({
            url: app.xhx_route.orderList,
          })
        }, 500)
        app.isToOrder = false;
      }
      that.getBalanceDetail();

    })
  },
  onShow: function() {
    var that = this
    const userInfo = wx.getStorageSync('user')
    if (userInfo) {
      this.setData({
        isLogin: true
      })
      app.xlogin.getUser(function (res) {
        var myInfo = app.isNavigateToLogin();
        if (!myInfo) return;

        myInfo = app.isNavigateToBind();
        if (!myInfo) return;

        app.infoLog("onShow isToOrder===>", app.isToOrder);
        that.getUser();
        that.setData({
          count: app.notifyCount
        })
        if (!app.notifyCount) {
          wx.hideTabBarRedDot({
            index: 2,
          })
        }
        if (app.isToOrder) {
          setTimeout(() => {
            wx.navigateTo({
              url: app.xhx_route.orderList,
            })
          }, 500)
          app.isToOrder = false;
        }
        that.getBalanceDetail();

      })
    }
    // app.infoLog("onShow isToOrder===>", app.isToOrder);
    // that.getUser();
    // that.setData({
    //   count: app.notifyCount
    // })
    // if (!app.notifyCount) {
    //   wx.hideTabBarRedDot({
    //     index: 2,
    //   })
    // }
    // if (app.isToOrder){
    //   setTimeout(()=>{
    //     wx.navigateTo({
    //       url: app.xhx_route.orderList,
    //     })
    //   },500)
    //   app.isToOrder = false;
    // }
  },
  login,
  tapPage,
  navToNext,
  getUser,
  navigateToMiniProgram,
  getBalanceDetail,
};
function login() {
  const that = this;
  app.xlogin.getUser(function (res) {
    var myInfo = app.isNavigateToLogin();
    if (!myInfo) {
      that.setData({
        isLogin: false
      })
      return;
    }

    myInfo = app.isNavigateToBind();
    if (!myInfo) {
      that.setData({
        isLogin: false
      })
      return;
    } else {
      that.setData({
        isLogin: true
      })
    }
    that.getUser()
  })
}

function tapPage() {
  const userInfo = wx.getStorageSync('user')
  if (!userInfo) {
    this.login()
  } else {
    this.setData({
      isLogin: true
    })
    that.getUser()
  }
}
var page = Object.assign({},currentPage);

Page(page);

function navToNext(event) {
  const that = this;

  var myInfo = app.isNavigateToLogin();
  if (!myInfo) return;

  myInfo = app.isNavigateToBind();
  if (!myInfo) return;

  let params = event.currentTarget.dataset;
  const url = params.url;
  const unskip = params.unskip;
  if (unskip) return false;
  wx.navigateTo({
    url: `${app.xhx_route[url]}?${paramFormat(params)}`,
  })
  
}

function paramFormat(params) {
  let url = '';
  for (let key in params) {
    if (key == 'url' || key == 'unskip') continue;
    if (url) url += `&`;
    url += `${key}=${params[key] || ''}`
  }

  return url;
}


function getUser() {
  const that = this;
  app.middleware.requestAjax({
    serAddr: app.serAddrs.getPersonCenterInfo,
    cancelToast: true,
    success: function(res) {
      var temp = res.data;
      that.setData(temp)
    },
    fail: function(res) {
      app.middleware.showToast(res && res.msg || "加载失败");
    }
  })
}

function navigateToMiniProgram(event) {
  const that = this;
  const appId = event.currentTarget.dataset.appid;
  const path = event.currentTarget.dataset.path;

  wx.navigateToMiniProgram({
    appId,
    path,
  })
}

function getBalanceDetail() {
  const that = this;
  app.middleware.requestAjax({
    serAddr: app.serAddrs.getBalanceDetail,
    data: {},
    cancelToast: true,
    success: function (res) {
      // let temp = res.data;
      // that.setData(temp)
      that.setData({
        balanceAmout: res.data.balanceAmout
      })
    },
    fail: function (res) {
      app.middleware.showToast(res && res.msg || "加载失败");
    }
  })
}